package atguigu.blockQueue;

import java.util.concurrent.ArrayBlockingQueue;

public class Test1 {

    public static void main(String[] args) {
        ArrayBlockingQueue<Integer> bq = new ArrayBlockingQueue<Integer>(5);
        // 一个线程负责放
        new Thread(new R1(bq)).start();
        // 一个线程负责取
        new Thread(new R2(bq)).start();
    }
    
    static class R1 implements Runnable{
    	ArrayBlockingQueue<Integer> bq;
    	public R1(ArrayBlockingQueue<Integer> bq) {
    		this.bq = bq;
    	}
		@Override
		public void run() {
			for (int i = 0; i < 10; i++) {
				bq.add(i);
				System.out.println("放入:"+i);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
    }


    static class R2 implements Runnable{
    	ArrayBlockingQueue<Integer> bq;
    	public R2(ArrayBlockingQueue<Integer> bq) {
    		this.bq = bq;
    	}
    	@Override
		public void run() {
    		while (true) {
    			Integer value = bq.poll();
    			System.out.println("取出:"+value);
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
			}
		}
    }
}
